package com.bryantkobe.myspringcode.exception.massert;

import com.bryantkobe.myspringcode.exception.ExceptionEnum;
import com.bryantkobe.myspringcode.factory.BusinessExceptionFactory;
import org.springframework.stereotype.Component;

/**
 * @description: 静态断言工具类 - 便于在非Spring管理的类中使用
 * @author: alienwarezzl
 * @date: 2025/9/23
 */
@Component
public class StaticBusinessAssert {
    private static BusinessExceptionFactory exceptionFactory;

    public StaticBusinessAssert(BusinessExceptionFactory exceptionFactory) {
        StaticBusinessAssert.exceptionFactory = exceptionFactory;
    }

    public static void checkArgument(boolean expression, ExceptionEnum exceptionEnum, Object... args) {
        if (!expression) {
            throw exceptionFactory.create(exceptionEnum, args);
        }
    }

    public static void checkState(boolean state, ExceptionEnum exceptionEnum, Object... args) {
        if (!state) {
            throw exceptionFactory.create(exceptionEnum, args);
        }
    }

    public static <T> T checkNotNull(T reference, ExceptionEnum exceptionEnum, Object... args) {
        if (reference == null) {
            throw exceptionFactory.create(exceptionEnum, args);
        }
        return reference;
    }
}
